#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#define UNIX_FILE  "/home/linux/CProject/network/UNIX/unixfile"
#define BACKLOG 5

int main(int args, const char * argv[]){

	int sockfd;
	int ret;
	struct sockaddr_un addr;
	// BUFSIZ 8192
	char buf[BUFSIZ] = {0};  
	//1. 创建套接字：IPV4、本地通信
	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if(sockfd < 0){
		perror("socket");
		exit(0);
	}

	//2. 绑定通信结构体
	addr.sun_family = AF_UNIX;
	//定义本地文件
	strncpy(addr.sun_path, UNIX_FILE, sizeof(addr.sun_path)-1);

	//3.客户端发起请求 
	if(connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1){
		perror("connect");
		exit(0);
	}

	//4. 客户端上送请求数据
	while(1){
		printf("Input->");
		fgets(buf, BUFSIZ, stdin);
		write(sockfd, buf, strlen(buf));
	}

	//6. 关闭套接字以及删除本地通信文件
	close(sockfd);
	return 0;
}
